package top.lyc829.aircontroller.ui.vidoeDetial;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import top.lyc829.aircontroller.R;
import top.lyc829.aircontroller.ui.BaseActivity;

public class VideoDetailActivity extends BaseActivity {

    private String name;
    private String url;
    private List<String> nameList;
    private TextView nameText;
    private ListView listView;
    private VideoDetailAdapter videoDetailAdapter;
    private Button downloadButton;
    private int processNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_detail);
        init();
    }

    private void init(){
        processNum = 30;
        nameList = new ArrayList<>();
        nameText = findViewById(R.id.name_text);
        downloadButton = findViewById(R.id.download);
        downloadButton.setOnClickListener(listener);
        listView = findViewById(R.id.list_view);
        videoDetailAdapter = new VideoDetailAdapter(this,R.layout.video_detail_item,nameList);
        listView.setAdapter(videoDetailAdapter);
        getInfo();
        getVideoDetail();
    }

    private void getInfo(){
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        url = intent.getStringExtra("url");
        nameText.setText(name);
    }

    private void getVideoDetail(){
        doGet(0,"getVideoDetail?url=" + url);
    }


    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String path = "downloadVideo?name=" + name + "&url=" + url + "&processNum=" + processNum;
            doGet(1,path);
        }
    };

    @Override
    protected void netCallBack(int flag,Object data) {
        if (flag == 0){
            List<String> list = (List<String>) data;
            nameList.clear();
            nameList.addAll(list);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    videoDetailAdapter.notifyDataSetChanged();
                }
            });
        }else if (flag == 1){

        }
    }
}